<template>
  <el-button
    key="download"
    :type="option.type ? option.type : 'default'"
    :ghost="option.ghost || false"
    :loading="loading"
    @click="handleDownload"
  >
    <template #icon>
      <el-icon><Download /></el-icon>
    </template>
    {{ option.title }}</el-button>
</template>
<script setup>
import { ref, defineProps } from 'vue'
import { message } from 'ant-design-vue'
import { Download } from '@element-plus/icons-vue'
import utils from '@/utils'
import { commonAPI } from '@/api'

const loading = ref(false)
const props = defineProps({
  option: {
    type: Object,
    default: () => ({
      title: '下载'
    })
  }
})
const handleDownload = async() => {
  loading.value = true
  utils.exportExcelUrl(props.option.url, props.option.filename, () => {
    message.success('导出成功')
    loading.value = false
  })
  //  return
  const params = {}
  const API_SOURCE = commonAPI
  const { apiFuc } = props.option
  // switch (apiFrom) {
  // 	case 'ihangzAPI':
  // 		API_SOURCE = ihangzAPI
  // 		break
  // 	case 'draftAPI':
  // 		API_SOURCE = draftAPI
  // 		break
  // 	case 'wordsAPI':
  // 		API_SOURCE = wordsAPI
  // 		break
  // 	default:
  // }
  if (!API_SOURCE[apiFuc]) {
    return
  }
  loading.value = true
  const [error, result] = await $to(API_SOURCE[apiFuc](params))
  loading.value = false
  if (error) {
    message.warn('导出失败')
    return
  }
  const title = props.option.filename + '-' + new Date().getTime()
  utils.exportExcel(result, title, () => {
    message.success('导出成功')
  })
}
</script>
